package com.yksj.healthtalk.ui.server;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.yksj.healthtalk.adapter.ServerOrderAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.ShopOrderEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.recharge.ChoiseDateActivity;
import com.yksj.healthtalk.utils.TimeUtil;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 订单查询
 * @author Administrator
 *
 */
public class ServerCenterOrderListActivity extends BaseFragmentActivity implements
OnClickListener, OnItemClickListener, OnRefreshListener2<ListView> {
	
	private PullToRefreshListView mPullToRefreshListView;
	private ListView mListView;
	private ServerOrderAdapter adapter;
	private int pagenum = 1;
	private int pagecount = 20;
	private List<ShopOrderEntity> entities = new ArrayList<ShopOrderEntity>();
	private String merchant_id = "";
	private String date = "";
	private TextView nowTime;// 日历当前显示时间
	private TextView buyRecord;
	private String GOODNAME;//按条件搜索名称

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.server_center_order_list);
		initWidget();
		initData();
	}

	private void initWidget() {
		initTitle();
		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.hall);
		mPullToRefreshListView.setOnRefreshListener(this);
		mListView = mPullToRefreshListView.getRefreshableView();
		mListView.setOnItemClickListener(this);
		mListView.setFooterDividersEnabled(true);
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setTextColor(Color.BLACK);
		titleRightBtn2.setBackgroundResource(R.drawable.icon_search);
		findViewById(R.id.calender).setOnClickListener(this);
		buyRecord = (TextView) findViewById(R.id.buy_record);
		titleTextV.setText("订单查询");
		adapter = new ServerOrderAdapter(this, entities);
		mListView.setAdapter(adapter);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode!=RESULT_OK)return;
		switch (requestCode) {
		case 0:
			if (data != null) {
				Bundle extras = data.getExtras();
				entities.clear();
				pagenum=1;
				if (extras.containsKey("all")) {
					date = "";
					initData();
				} else {
					if (extras.containsKey("nothing"))return;
					String string = extras.getString("date");
					String string1 = extras.getString("year");
					nowTime.setText(string1 + "-" + string);
					date = string1 + string;
					dohttp(merchant_id);
				}
			} else {
				date = null;
				nowTime.setText(TimeUtil.getMoneyTime().substring(0, 4) + "");
				dohttp(merchant_id);
			}
			break;
		case 1000:
			/*String GoodsName=getIntent().getStringExtra("Result");
			if((GoodsName!=null)&&(!GoodsName.equals(""))){
				GOODNAME=GoodsName;
				entities.clear();
				pagenum=1;
				dohttp(merchant_id);
				GOODNAME="";
			}*/
			break;
		}
	}


	private void initData() {
		nowTime = (TextView) findViewById(R.id.now_time);
		nowTime.setText(TimeUtil.getMoneyTime().substring(0, 4) + "");
		nowTime.setOnClickListener(this);
		dohttp(merchant_id);
	}

	private void dohttp(String merchant_id) {
		HttpRestClient.doHttpYAOFANGWANGSERVLET(merchant_id, pagenum,
				pagecount, date,GOODNAME, new JsonsfHttpResponseHandler(this) {
					@Override
					public void onSuccess(com.alibaba.fastjson.JSONObject object) {
						try {
							if (pagenum == 1)entities.clear();
							if(!"0".equals(object.getString("COUNT"))){
								entities.addAll(JSONArray.parseArray(object.getJSONArray("INFO").toString(),
										ShopOrderEntity.class));
								adapter.notifyDataSetChanged();
								pagenum++;
							}
							buyRecord.setText("详细记录(共"+ object.getString("COUNT") + "条)");
							if (entities.size() == 0)
								ToastUtil.showToastPanl("没有记录");
						} catch (Exception e) {
							mPullToRefreshListView.onRefreshComplete();
						}
					}


					@Override
					public void onFinish() {
						mPullToRefreshListView.onRefreshComplete();
						super.onFinish();
					}
				});
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2:
			intent = new Intent(this, ServerCenterOriderForGoodsSeachActivity.class);
			startActivityForResult(intent, 1000);
			break;
		case R.id.calender:
		case R.id.now_time:
			intent = new Intent(this, ChoiseDateActivity.class);
			if (nowTime.getText().toString().length() < 5) {
				intent.putExtra("date", TimeUtil.getMoneyTime());
			} else {
				intent.putExtra("date", nowTime.getText().toString());
			}
			startActivityForResult(intent, 0);
			break;
		}
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		pagenum = 1;
		dohttp(merchant_id);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		dohttp(merchant_id);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (1 <= arg2 && arg2 <= entities.size()) {
			Intent intent = new Intent(getApplicationContext(),
					ServerCenterOrderDetailListActivity.class);
			intent.putExtra("merchant_id", merchant_id);
			intent.putExtra("order_id", entities.get(arg2 - 1).getORDER_ID());
			startActivity(intent);
		}
	}
}
